/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function() {
        var param = $(this).val().trim();
        var val = $("input:checkbox[name='sex']:selected").val();
        if (val == 1) {
            searchParams['sex'] = 1;
        } else if (val == 2) {
            searchParams['sex'] = 2;
        }
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('name')] = param;
    });
    return searchParams;
}

function getSearchParams2(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    return searchParams;
}
//清空搜索条件
$("#Reset").click(function() {
    $('.SearchForm').val('');
    $("input:radio[name='sex']").removeAttr('checked');
});

/**
 * 页面自加载
 */
$(function() {
    // $('#student_createtime').datepicker({
    //     format: "yyyy-mm-dd",
    //     todayBtn: "linked",
    //     autoclose: true,
    //     todayHighlight: true,
    //     clearBtn: true
    // });

    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_studentinfo').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_studentinfo').datagrid({
        singleSelect: true, //允许选择多行
        striped: true,
        idField: 'id',
        remoteSort: false,
        collapsible: true,
        fit: false,
        url: 'admin/User/getUserData', //数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[{
            field: 'user_username',
            title: '用户名',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'user_gender',
            title: '性别',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter: function (value) {
                if (value == 1) {
                    return "男";
                }
                if (value == 2) {
                    return "女";
                }
            }
        }, {
            field: 'user_phone',
            title: '手机号',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'user_time',
            title: '注册时间',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'user_status',
            title: '状态',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter: function (value) {
                if (value == 1) {
                    return "<span style='color:green;font-weight: bold'>正常</span>";
                }
                if (value == 2) {
                    return "<span style='color:red;font-weight: bold'>冻结</span>";
                }
            }
        }, {
            field: 'user_id',
            title: '操作',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function(value,row,index){
                var html ='';
                if(row.user_status == 1){
                    html ="<a onclick='TyInfo(this);' style='color:#F17A02;margin-left:5px;font-weight: bold' name='"+row.user_id+"' href='JavaScript:;'>禁用</a>&nbsp;&nbsp;&nbsp;";
                }else{
                    html ="<a onclick='QyInfo(this);' style='margin-left:5px;font-weight: bold' name='"+row.user_id+"' href='JavaScript:;'>启用</a>&nbsp;&nbsp;&nbsp;";
                }
                return html;
            }
        }]],
        onBeforeLoad: function (param) {
            param = getSearchParams(param);
        },
    });

    $('#Search').click(function() {
        $('#datatable_studentinfo').datagrid('reload');
    });

    /**
     * 删除
     */
    $('#del').click(function() {
        var row = $('#datatable_studentinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '只能选择一行进行修改！',
            });
            return;
        }
        $.TeachDialog({
            content: "确认删除此用户？",
            showCloseButton: true,
            showCloseButtonName: '取消',
            otherButtons: ['确认'],
            otherButtonStyles: ['btn-danger'],
            clickButton: function(sender, modal, index) {
                modal.modal('hide');
                $.ajax({
                    url: 'Admin/User/deleteUser', //form action
                    dataType: 'JSON', //返回体类型
                    type: 'POST', // form type
                    data: "id=" + row[0].user_id,
                    success: function(data) {
                        if (data.status == 1) {
                            // $.TeachDialog({
                            //     content: data.msg,
                            // });
                            layer.msg(data.msg,{icon:6,time:2000});
                            $('#datatable_studentinfo').datagrid('reload');
                            $('#datatable_studentinfo').datagrid('uncheckAll');
                        } else {
                            // $.TeachDialog({
                            //     content: '删除失败',
                            // });
                            layer.msg(data.msg,{icon:5,time:2000});
                        }
                    },
                    error: function() {
                        // $.TeachDialog({
                        //     content: '删除失败',
                        // });
                        layer.msg("删除失败!",{icon:5,time:2000});
                    }
                });
            }
        })
    });

    /**
     * 查看学生详情
     */
    $("#look").click(function() {
        var row = $('#datatable_studentinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一个用户进行查看！'
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '请选择一个用户进行查看！',
            });
            return;
        }
        //获得修改框内容
        var content = "";
        $.ajax({
            url:"admin/User/seeUserInfo",
            data:"id=" + row[0].user_id,
            type:'POST',
            dataType:'HTML',
            success:function(returndata){
                content = returndata;
                $.TeachDialog({
                    modalId: null,
                    animation: null,
//                        largeSize:true,
                    title: "<h2>用户详情</h2>",
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function () {
                    }
                });
            },
            error:function(){
                alert('数据返回出错，无伤大雅，请继续');
            }
        });

    });

    /**
     * 查看学生发布的职位
     */
    $("#myjobs").click(function() {
        var row = $('#datatable_studentinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一个用户进行查看！'
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '请选择一个用户进行查看！',
            });
            return;
        }
        //获得修改框内容
        var content = "";
        $.ajax({
            url:"admin/User/myJobs",
            data:"uid=" + row[0].user_id,
            type:'POST',
            dataType:'HTML',
            success:function(returndata){
                content = returndata;
                layer.open({
                    type: 1,
                    title: ['用户发布的职位','font-size:20px;'],
                    closeBtn: 2,
                    area: ['1000px','500px'],
                    shadeClose: false,
                    zIndex:998,
                    content: content
                });
            },
            error:function(){
                alert('数据返回出错，无伤大雅，请继续');
            }
        });

    });
    /**
     * 查看学生的评论
     */
    $("#mycomments").click(function() {
        var row = $('#datatable_studentinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一个用户进行查看！'
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '请选择一个用户进行查看！',
            });
            return;
        }
        //获得修改框内容
        var content = "";
        $.ajax({
            url:"admin/User/myComments",
            data:"uid=" + row[0].user_id,
            type:'POST',
            dataType:'HTML',
            success:function(returndata){

                content = returndata;
                $.TeachDialog({
                    modalId: null,
                    animation: null,
                    largeSize:true,
                    title: "<h2>用户评论</h2>",
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function () {
                    }
                });
                //显示评论列表
                var psval = $('#datatable_studentinfo2').attr('data-size');
                if (psval == undefined || psval == "") {
                    psval = 10;
                }
                var uid = $('#uid').val();
                $('#datatable_studentinfo2').datagrid({
                    singleSelect: true, //允许选择多行
                    striped: true,
                    idField: 'comment_id',
                    remoteSort: false,
                    collapsible: true,
                    fit: false,
                    url: 'admin/User/myCommentsTable', //数据源路径
                    queryParams:{
                      uid:uid
                    },
                    loadMsg: '请等待数据载入....',
                    pagination: true,
                    rownumbers: true,
                    fitColumns: true,
                    pageSize: psval,
                    pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
                    columns: [[{
                        field: 'user_username',
                        title: '评论者名字',
                        align: 'center',
                        width: cellwidth,
                        sortable: true
                    }, {
                        field: 'comment_content',
                        title: '评论内容',
                        align: 'center',
                        width: cellwidth,
                        sortable: true,
                    }, {
                        field: 'comment_score',
                        title: '评论给分',
                        align: 'center',
                        width: cellwidth*0.5,
                        sortable: true
                    }, {
                        field: 'comment_type',
                        title: '评论类型',
                        align: 'center',
                        width: cellwidth*0.7,
                        sortable: true,
                        formatter: function (value) {
                            if (value == 1) {
                                return "个人";
                            }
                            if (value == 2) {
                                return "企业";
                            }
                            return "找活干";
                        }
                    }, {
                        field: 'comment_time',
                        title: '评论时间',
                        align: 'center',
                        width: cellwidth*1.3,
                        sortable: true,
                    }, {
                        field: 'comment_id',
                        title: '操作',
                        align: 'center',
                        width: cellwidth*1.5,
                        sortable: true,
                        formatter:function(value,row,index){
                            var c = row.comment_content;
                            return "<a onclick='comitInfo(this);' style='margin-left:5px;font-weight: bold' name='"+value+"' href='JavaScript:;'>查看回复</a>"+
                             "<a onclick='kancom(this);' style='margin-left:5px;font-weight: bold;' name='"+c+"' href='JavaScript:;'>评论内容</a>"+
                             "<a onclick='deleteCom(this);' style='margin-left:5px;font-weight: bold;color: red' name='"+value+"' href='JavaScript:;'>删除</a>";
                        }
                    }]],
                    onBeforeLoad: function (param) {
                        param = getSearchParams2(param);
                    },
                });
                //=============================================
            },
            error:function(){
                alert('数据返回出错，无伤大雅，请继续');
            }
        });

    });



});